package com.google.android.apps.authenticator;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RemoteViews;
import android.widget.ToggleButton;

import com.google.android.apps.authenticator.testability.DependencyInjector;
import com.google.android.apps.authenticator2.R;

public class ConfigureAuthenticatorWidgetActivity extends Activity {
	
	private AccountDb mAccountDb;
	private int mWidgetID;
	private Context mContext;
	public static final String WIDGET_SHARED_PREFERENCE_KEY_PREFIX = "com.google.android.apps.authenticator.WIDGET_SHARED_PREFERENCE_";
	public static final String WIDGET_SELECTED_ACCOUNT_KEY = "com.google.android.apps.authenticator.WIDGET_SELECTED_ACCOUNT_KEY";
	public static final String WIDGET_CONFIG_COMPLETE = "com.google.android.apps.authenticator.WIDGET_CONFIG_COMPLETE";
	@Override 
	public void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		
		mContext = this;
		
		setResult(RESULT_CANCELED);
		setContentView(R.layout.configure_widget_view);
		
		mAccountDb = DependencyInjector.getAccountDb();
		
		ArrayList<String> accounts = new ArrayList<String>();
		mAccountDb.getNames(accounts);
		
		LinearLayout selectAccountArea = (LinearLayout) findViewById(R.id.select_account_area);
		
		Intent intent = this.getIntent();
		Bundle extras = intent.getExtras();
		
		mWidgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
		
		if(extras != null) {
			mWidgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}
		
		if(mWidgetID == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}
		
		
		for(String account : accounts) {
		     ToggleButton selectAccountButton = new ToggleButton(this);
			 selectAccountButton.setText(account);
			 selectAccountButton.setTag(account);
			 selectAccountButton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			 selectAccountButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {	
					
					RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.widget_view);
					AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
					
					appWidgetManager.updateAppWidget(mWidgetID, views);
					
					Intent resultValue = new Intent();
					resultValue.setAction(WIDGET_CONFIG_COMPLETE);
					resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetID);
					resultValue.putExtra(ConfigureAuthenticatorWidgetActivity.WIDGET_SELECTED_ACCOUNT_KEY, (String)view.getTag());
					setResult(RESULT_OK, resultValue);
					PendingIntent pintent = PendingIntent.getBroadcast(mContext, 0, resultValue, PendingIntent.FLAG_UPDATE_CURRENT);
					AlarmManager alarmsvc = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
					alarmsvc.set(AlarmManager.RTC, System.currentTimeMillis() + 200, pintent);
					
					finish();
				 }
			 });
			 
			 selectAccountArea.addView(selectAccountButton);
		}
		
	}
}
